package com.css.msgcenter.admin.msgapp.action;

import java.sql.Timestamp;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.dict.action.DirDict;
import com.css.core.configuration.Environment;
import com.css.core.model.IServiceResult;
import com.css.msgcenter.admin.msgapp.service.MsgAppService;
import com.css.msgcenter.server.model.MsgApp;
import com.css.util.Ajax;
import com.css.util.DesUtil;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ModelDriven;

public class UpdMsgApp extends AdminAction implements ModelDriven{
	private static Log log = LogFactory.getLog(DirDict.class);
	private String ids;
	private String openStatus;
	private MsgApp msgApp;
	private String mailSmtpHost;
	private String mailUser;
	private String mailPassword;
	private String ips;
	private String dateInfo;
	
	public UpdMsgApp() {
		msgApp = new MsgApp();
	}
	@Override
	public String adminGo() {
		IServiceResult<?> res = null;
		if (StringHelper.isEmpty(ids) || StringHelper.isEmpty(openStatus)) {
			if(msgApp !=null ){
				if(StringHelper.isEmpty(ips))
					ips="";
				Timestamp createTime = new Timestamp(System.currentTimeMillis()); 
				msgApp.setCreatedTime(createTime.valueOf(dateInfo));
				String memo = "{\"mail.smtp.host\":\"" + mailSmtpHost + "\",\"mail.user\":\"" + mailUser +
					"\",\"mail.password\":\"" + DesUtil.encrypt(mailPassword) + "\"}";
				msgApp.setMemo(memo);
				String MsgType = "";
				if(StringHelper.isNotEmpty(msgApp.getCheckMsgType())){
					List<String> MsgTypeList = StringHelper.strToList(msgApp.getCheckMsgType());
					for(int i = 0; i < MsgTypeList.size(); i++){
						MsgType += MsgTypeList.get(i).trim() + ",";
					}
					MsgType=MsgType.substring(0, MsgType.length()-1);
				}
				msgApp.setCheckMsgType(MsgType);
				res = MsgAppService.updMsgApp(msgApp,ips);
			}else{
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
				return Action.ERROR;
			}
		}else{
			res = MsgAppService.updMsgApp(ids,openStatus);
		}
		result = Ajax.JSONResult(res.getResultCode(), res.getResultDesc(),res.getResultObject());
		return res.toActionResult();
	}
	@Override
	public Object getModel() {
		return msgApp;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public String getOpenStatus() {
		return openStatus;
	}
	public void setOpenStatus(String openStatus) {
		this.openStatus = openStatus;
	}
	public MsgApp getMsgApp() {
		return msgApp;
	}
	public void setMsgApp(MsgApp msgApp) {
		this.msgApp = msgApp;
	}
	public String getMailSmtpHost() {
		return mailSmtpHost;
	}
	public void setMailSmtpHost(String mailSmtpHost) {
		this.mailSmtpHost = mailSmtpHost;
	}
	public String getMailUser() {
		return mailUser;
	}
	public void setMailUser(String mailUser) {
		this.mailUser = mailUser;
	}
	public String getMailPassword() {
		return mailPassword;
	}
	public void setMailPassword(String mailPassword) {
		this.mailPassword = mailPassword;
	}
	public String getIps() {
		return ips;
	}
	public void setIps(String ips) {
		this.ips = ips;
	}
	public String getDateInfo() {
		return dateInfo;
	}
	public void setDateInfo(String dateInfo) {
		this.dateInfo = dateInfo;
	}
	


}